package phoenics.apeak.simple.service;

import java.util.ArrayList;
import java.util.List;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.OptionHandlerFilter;
import org.kohsuke.args4j.spi.BooleanOptionHandler;

import phoenics.apeak.crawl.Crawl;
import phoenics.apeak.simple.config.SimpleConfig;
public class SimpleMain {
	@Option(name="-h",usage="MongoDB host")
	private String host="localhost";
	@Option(name="-p",usage="MongoDB host port")
	private int port=27017;
	@Option(name="-c",usage="MongoDB collection for sellshow")
	private String collection="sellshow";
	@Option(name="-m",usage="MongoDB collection for manager")
	private String managercollection="manager";
	@Option(name="-n", handler=BooleanOptionHandler.class,usage="Initialization collection sellshow")
	private boolean renew=false;
	 @Argument
	private List<String> arguments = new ArrayList<String>();
	public static void main(String[] args) throws InterruptedException {
		new SimpleMain().doMain(args);
	}

	public void doMain(String[] args) throws InterruptedException {
		CmdLineParser parser = new CmdLineParser(this);
		try {
			parser.parseArgument(args);
			 doRun();
		} catch (CmdLineException e) {
            System.err.println(e.getMessage());
            System.err.println("java SampleMain [options...] arguments...");
            parser.printUsage(System.err);
            System.err.println();
            // print option sample. This is useful some time
            System.err.println(" Example: java SampleMain"+ parser.printExample(OptionHandlerFilter.ALL));
            return;
		}
	}
	
	private void doRun() throws InterruptedException {
		SimpleConfig.host=this.host;
		SimpleConfig.port=this.port;
		SimpleConfig.collection=this.collection;
		SimpleConfig.managerCollection=this.managercollection;
		Crawl crawl=new Crawl();
		if(renew){
			crawl.runCrawl4Ini(SimpleConfig.class, FireUrl.class);
		}
		crawl.runCrawl(SimpleConfig.class, FireUrl.class);
	}
}
